package com.ql.service.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ql.dao.CementMixingDao;
import com.ql.dao.SectionDao;
import com.ql.domain.CementMixing;
import com.ql.domain.Device;
import com.ql.domain.Section;
import com.ql.domain.request.ConditionQuery;
import com.ql.domain.request.PermissionQuery;
import com.ql.domain.response.JsonResult;
import com.ql.domain.response.OperationResult;
import com.ql.service.CementMixingService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class CementMixingServiceImpl implements CementMixingService {
    @Autowired
    CementMixingDao cementMixingDao;
    @Autowired
    SectionDao sectionDao;

    /**
     * 获取桩机号分类
     *
     * @return
     */
    @Override
    public List<String> findPileNum() {
        return cementMixingDao.findPileNum();
    }

    /**
     * product数据表格多条件查询及分页
     *
     * @param conditionQuery
     * @param page
     * @param limit
     * @return
     */
    @Override
    public JsonResult findByCriteria(ConditionQuery conditionQuery, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<CementMixing> cementMixingList = cementMixingDao.findByCriteria(conditionQuery);
        for (CementMixing cementMixing : cementMixingList) {
            //获得搅拌阶段  1或者0 设置初搅深度 和复搅深度
            String phase = cementMixing.getPhase();
            if (phase != null && cementMixing.getDrilling_depth() != null) {
                if (phase.equals("0")) {
                    cementMixing.setCjsd(cementMixing.getDrilling_depth());
                    cementMixing.setFjsd("0.00");
                } else if (phase.equals("1")) {
                    cementMixing.setFjsd(cementMixing.getDrilling_depth());
                    cementMixing.setCjsd("0.00");
                }
            } else {
                cementMixing.setFjsd("0.00");
                cementMixing.setCjsd("0.00");
            }
            //获取 桩端电流
            cementMixing.setZddl("0.00");
            //获取最大电流
            String maxDl = cementMixingDao.findMaxDl(cementMixing.getStake_mark());
            cementMixing.setMaxdl(maxDl);
            String pile_driver_state = cementMixing.getPile_driver_state();
            if (pile_driver_state != null && cementMixing.getDrilling_speed() != null) {
                if (pile_driver_state.equals("1")) {
                    //获取最大下钻速
                    cementMixing.setZdzs(cementMixingDao.findMaxSpeed(cementMixing.getStake_mark()));
                } else if (pile_driver_state.equals("2")) {
                    //获取最大提速
                    cementMixing.setZdts(cementMixingDao.findMaxSpeed(cementMixing.getStake_mark()));
                } else if (pile_driver_state.equals("0")) {
                    cementMixing.setZdts("0.00");
                    cementMixing.setZdzs("0.00");
                }
            }
            //最大斜度
            cementMixing.setZdxd(cementMixingDao.findMaxSlope(cementMixing.getStake_mark()));
            //扩大头桩长
            cementMixing.setKdtzc("0.00");
            //扩大头浆量
            cementMixing.setKdtjl("0.00");
            //扩大头灰量
            cementMixing.setKdthl("0.00");
            //下部桩浆量
            cementMixing.setXbzjl("0.00");
            //下部桩灰量
            cementMixing.setXbzhl("0.00");
            //预警级别
            cementMixing.setYjjb("无预警");

        }
        PageInfo pageInfo = new PageInfo(cementMixingList);
        String totalPiles = cementMixingDao.findTotalPiles(conditionQuery.getStakeMark());
        //截取小数点后两位
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        String totalPileLength = cementMixingDao.findTotalPileLength();
        totalPileLength = decimalFormat.format(new BigDecimal(totalPileLength));
        String sumAoc = cementMixingDao.findSumAoc();
        sumAoc = decimalFormat.format(new BigDecimal(sumAoc));
        String sumAol = cementMixingDao.findSumAol();
        sumAol = decimalFormat.format(new BigDecimal(sumAol));
        return JsonResult.success(pageInfo.getList(), pageInfo.getTotal(), totalPiles, totalPileLength, sumAoc, sumAol);
    }

    @Override
    public JsonResult findOperationAll(PermissionQuery permissionQuery, CementMixing cementMixing, Integer page, Integer limit) {

        //页面首次进入,根据userId查询其权限关联的设备
        if (StringUtils.isEmpty(cementMixing.getDevice_code())) {
            //进行分页
            List<OperationResult> operationResultList = new ArrayList<>();
            //如果userId不为空并且projectId和sectionId为空
            if (StringUtils.isNotEmpty(permissionQuery.getUserId()) && StringUtils.isEmpty(permissionQuery.getProjectId()) && StringUtils.isEmpty(permissionQuery.getSectionId())) {
                //根据userId查询出对应的设备id
                List<Section> sectionList = sectionDao.findDeviceByUserId(permissionQuery.getUserId());
                PageHelper.startPage(page, limit);
                return disposeList(sectionList, cementMixing);
            }
            //如果projectId不为空,sectionId为空
            else if (StringUtils.isNotEmpty(permissionQuery.getProjectId()) && StringUtils.isEmpty(permissionQuery.getSectionId())) {
                //根据projectId查询对应的设备id
                List<Section> sectionList = sectionDao.findByProjectId(permissionQuery.getProjectId());
                PageHelper.startPage(page, limit);
                return disposeList(sectionList, cementMixing);
            } else if (StringUtils.isNotEmpty(permissionQuery.getSectionId())) {
                List<Section> sectionList = sectionDao.findBySectionId(permissionQuery.getSectionId());
                PageHelper.startPage(page, limit);
                return disposeList(sectionList, cementMixing);
            }
        } else {
            //如果设备编号不为空
            PageHelper.startPage(page, limit);
            List<OperationResult> operationResultList = cementMixingDao.findByCondition(cementMixing);
            OperationResult operationTotal = cementMixingDao.findOperationTotal(null, cementMixing);
            PageInfo pageInfo = new PageInfo(operationResultList);
            return JsonResult.success(pageInfo.getList(), pageInfo.getTotal(), operationTotal.getTotalPiles(), operationTotal.getTotalPileLength(), null, null);
        }
        return JsonResult.error();
    }

    private JsonResult disposeList(List<Section> sectionList, CementMixing cementMixing) {
        if (sectionList != null) {
            //保存设备id
            ArrayList<String> devNums = new ArrayList<>();
            for (Section section : sectionList) {
                List<Device> devices = section.getDevices();
                for (Device device : devices) {
                    //获得用户关联的所有设备id
                    devNums.add(device.getDevNum());
                }
            }
            if (devNums.size() == 0) {
                return JsonResult.error();
            }
            //处理查询结果,将进度和当前作业查询出并赋值
            List<OperationResult> operationResultList = cementMixingDao.findByPileDriverNum(devNums, cementMixing);
            OperationResult operationTotal = cementMixingDao.findOperationTotal(devNums, cementMixing);
            PageInfo pageInfo = new PageInfo(operationResultList);
            return JsonResult.success(pageInfo.getList(), pageInfo.getTotal(), operationTotal.getTotalPiles(), operationTotal.getTotalPileLength(), null, null);
        }
        return JsonResult.error();
    }
   /* public String findTotalPiles() {
        return cementMixingDao.findTotalPiles();
    }

    @Override
    public String findTotalPileLength() {
        return cementMixingDao.findTotalPileLength();
    }

    @Override
    public String findSumAoc() {
        return cementMixingDao.findSumAoc();
    }
    @Override
    public String findSumAol() {
        return cementMixingDao.findSumAol();
    }*/
}
